﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CoyoEden.Web.Data;
using CoyoEden.Models;
using System.Web;
using System.IO;
using Vivasky.Core.Infrastructure;

namespace CoyoEden.Web.Controller
{
	/// <summary>
	/// bf for PortalFile
	/// </summary>
	public class PortalFileController:ControllerBase
	{
		public static void Update(List<NameValuePair<string, string>> Params,out FeedbackData msg){
			msg = new FeedbackData();
			var id = Params.Find(item=>item.name=="FileId").value;
			var name = Params.Find(item=>item.name=="FileName").value;
			var desc = Params.Find(item=>item.name=="FileDescription").value;
			PortalFile file = PortalFile.PortalFiles.Find(item=>item.Id.ToString()==id);
			if (file == null) {
				msg.IsOk = false;
				msg.Body = "无法找到待更新的文件.";
				return;
			}
			//do save
			file.Name = name.Trim();
			file.Description = desc;
			try { 
				file.Save(out msg);
				msg.Context.Add("data",file);
			}catch(Exception ex){
				msg.IsOk = false;
				msg.Body = "更新文件时发生例外";
				msg.Context.Add("error",ex.Message);
			}

		}
		public static void Delete(Dictionary<string, string> Params, out FeedbackData msg)
		{
			msg = new FeedbackData();
			var id=Params["id"];
			PortalFile file = PortalFile.PortalFiles.Find(item=>item.Id.ToString()==id);
			if (file == null) {
				msg.IsOk = false;
				msg.Body = "无法找到带删除的文件";
				return;
			}
			file.Delete();
			try
			{
				file.Save(out msg);
				//delete from disk
				//var path = HttpContext.Current.Request.MapPath(file.Url);
				//if (File.Exists(path)) {
				//  File.Delete(path);
				//}
			}
			catch (Exception ex) {
				msg.IsOk = false;
				msg.Body = "删除文件时发生例外";
				msg.Context.Add("error",ex.Message);
			}
		}
	}
}
